package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.Employee;
import cn.itsource.ymcc.domain.Login;
import cn.itsource.ymcc.errorcode.ErrorCode;
import cn.itsource.ymcc.exception.GlobalException;
import cn.itsource.ymcc.feignclient.SystemFeignClient;
import cn.itsource.ymcc.mapper.LoginMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.ILoginService;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.util.jwtutil.JwtUtils;
import cn.itsource.ymcc.util.jwtutil.PayloadData;
import cn.itsource.ymcc.vo.LoginVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author Mr.xiang
 * @since 2023-09-11
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private SystemFeignClient systemFeignClient;

    private final static Integer EXPIRE = 24 * 60 * 60;

    /**
     * LambdaQueryWrapper  条件构造器，支持lambda表达式
     * QueryWrapper     条件构造器 不支持lambda表达式
     *
     * @param loginVo 登录信息
     * @return
     */
    @Override
    public Map<String, Object> accountLogin(LoginVo loginVo) {

        Login login = loginMapper.selectOne(new LambdaQueryWrapper<Login>()
                .eq(Login::getUsername, loginVo.getUsername())
                .eq(Login::getType, loginVo.getType()));

        //断言工具类
        AssertUtil.isNotNull(login, "用户名或密码错误！");

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(loginVo.getPassword(), login.getPassword());

        AssertUtil.isTrue(matches, "用户名或密码错误！");

        Map<String, Object> map = new HashMap<>();

        //使用JWT生成Token
        PayloadData payloadData = new PayloadData();
        payloadData.setLogin(login);
        if (login.getType() == 0) {
            //是管理员则进来
            /*JsonResult jsonResult = systemFeignClient.getEmployeeByLoginId(login.getId());
            if (!jsonResult.getSuccess()) {
                throw new GlobalException(ErrorCode.SERVICE_CALL_ERROR);
            }
            Object data = jsonResult.getData();
            String jsonString = JSON.toJSONString(data);
            Employee employee = JSONObject.parseObject(jsonString, Employee.class);*/
            login.setPassword(null);
            payloadData.setAdmin(login);
            //查询菜单和权限

        } else {
            login.setPassword(null);
            payloadData.setUser(login);
        }
        String token = JwtUtils.getToken(payloadData, EXPIRE);
        map.put("token", token);
        map.put("admin", payloadData.getAdmin());
        map.put("user",payloadData.getUser());
        //数据脱敏


        return map;
    }
}
